<?php
// 开启会话
session_start();

// 初始化学校数据
if (!isset($_SESSION['school'])) {
    $_SESSION['school'] = [
        'money' => 10000, // 初始资金
        'teachers' => 0,  // 教师数量
        'students' => 0,  // 学生数量
        'facility_level' => 1, // 设施等级
        'reputation' => 50 // 学校声誉
    ];
}

// 处理招聘教师操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['hire_teacher'])) {
    $cost = 1000;
    if ($_SESSION['school']['money'] >= $cost) {
        $_SESSION['school']['money'] -= $cost;
        $_SESSION['school']['teachers']++;
        $_SESSION['school']['reputation'] += 5;
        $message = "成功招聘一名教师，花费 $cost 元，学校声誉提升 5 点。";
    } else {
        $message = "资金不足，无法招聘教师。";
    }
}

// 处理招生操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recruit_student'])) {
    $max_students = $_SESSION['school']['teachers'] * 10; // 教师数量决定最大招生数
    if ($_SESSION['school']['students'] < $max_students) {
        $_SESSION['school']['students']++;
        $_SESSION['school']['money'] += 500;
        $_SESSION['school']['reputation'] += 2;
        $message = "成功招收一名学生，收入 500 元，学校声誉提升 2 点。";
    } else {
        $message = "学校教师数量不足，无法再招收更多学生。";
    }
}

// 处理建设设施操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['build_facility'])) {
    $cost = 2000 * $_SESSION['school']['facility_level'];
    if ($_SESSION['school']['money'] >= $cost) {
        $_SESSION['school']['money'] -= $cost;
        $_SESSION['school']['facility_level']++;
        $_SESSION['school']['reputation'] += 10;
        $message = "成功建设设施，等级提升到 ". $_SESSION['school']['facility_level']. "，花费 $cost 元，学校声誉提升 10 点。";
    } else {
        $message = "资金不足，无法建设设施。";
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟校长学校经营游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>

<body>
    <h1>模拟校长学校经营游戏</h1>
    <p>当前资金: <?php echo $_SESSION['school']['money']; ?> 元</p>
    <p>教师数量: <?php echo $_SESSION['school']['teachers']; ?> 名</p>
    <p>学生数量: <?php echo $_SESSION['school']['students']; ?> 名</p>
    <p>设施等级: <?php echo $_SESSION['school']['facility_level']; ?></p>
    <p>学校声誉: <?php echo $_SESSION['school']['reputation']; ?> 点</p>

    <?php if (isset($message)): ?>
        <div class="message <?php echo strpos($message, '成功')!== false? 'success' : 'error'; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="post">
        <button type="submit" name="hire_teacher">招聘教师（1000 元/人）</button>
    </form>

    <form method="post">
        <button type="submit" name="recruit_student">招生（收入 500 元/人）</button>
    </form>

    <form method="post">
        <button type="submit" name="build_facility">建设设施（当前花费 <?php echo 2000 * $_SESSION['school']['facility_level']; ?> 元）</button>
    </form>
</body>

</html>